package com.ruoyi.web.controller.modules;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RolesEnums;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.driver.domain.DriverInfo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.modules.domain.TransportationCompany;
import com.ruoyi.modules.service.ITransportationCompanyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 运输公司信息Controller
 * 
 * @author ssh
 * @date 2024-12-15
 */
@RestController
@RequestMapping("/modules/transportationCompany")
public class TransportationCompanyController extends BaseController
{
    @Autowired
    private ITransportationCompanyService transportationCompanyService;


    @Autowired
    private ISysUserService userService;

    /**
     * 查询运输公司信息列表
     */
    @PreAuthorize("@ss.hasPermi('modules:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(TransportationCompany transportationCompany)
    {

        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            transportationCompany.setUserId(user.getUserId()+"");
        }
        startPage();
        List<TransportationCompany> list = transportationCompanyService.selectTransportationCompanyList(transportationCompany);
        return getDataTable(list);
    }

    /**
     * 导出运输公司信息列表
     */
    @PreAuthorize("@ss.hasPermi('modules:company:export')")
    @Log(title = "运输公司信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TransportationCompany transportationCompany)
    {
        List<TransportationCompany> list = transportationCompanyService.selectTransportationCompanyList(transportationCompany);
        ExcelUtil<TransportationCompany> util = new ExcelUtil<TransportationCompany>(TransportationCompany.class);
        util.exportExcel(response, list, "运输公司信息数据");
    }

    /**
     * 获取运输公司信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('modules:company:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(transportationCompanyService.selectTransportationCompanyById(id));
    }

    /**
     * 新增运输公司信息
     */
    @PreAuthorize("@ss.hasPermi('modules:company:add')")
    @Log(title = "运输公司信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TransportationCompany transportationCompany)
    {
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            transportationCompany.setUserId(user.getUserId()+"");
        }
        return toAjax(transportationCompanyService.insertTransportationCompany(transportationCompany));
    }

    /**
     * 修改运输公司信息
     */
    @PreAuthorize("@ss.hasPermi('modules:company:edit')")
    @Log(title = "运输公司信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TransportationCompany transportationCompany)
    {
        return toAjax(transportationCompanyService.updateTransportationCompany(transportationCompany));
    }

    /**
     * 删除运输公司信息
     */
    @PreAuthorize("@ss.hasPermi('modules:company:remove')")
    @Log(title = "运输公司信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(transportationCompanyService.deleteTransportationCompanyByIds(ids));
    }

    /**
     * TODO 管理员审核车辆视频
     */
    @PreAuthorize("@ss.hasPermi('modules:company: ')")
    @GetMapping(value = "/getTranCompayCars")
    public AjaxResult getTranCompayCars(@PathVariable("id") String id)
    {
        return success(transportationCompanyService.selectTransportationCompanyById(id));
    }



    /**
     * todo
     * 运输公司信息--->绑定系统账号
     */
    @PreAuthorize("@ss.hasPermi('modules:company:banduser')")
    @Log(title = "运输公司信息--绑定系统账号", businessType = BusinessType.UPDATE)
    @PostMapping("/banduser")
    public AjaxResult banduser(@RequestBody TransportationCompany transportationCompany)
    {
        return toAjax(transportationCompanyService.updateTransportationCompany(transportationCompany));
    }


    /**
     * 运输公司绑定系统账号
     */
    @PreAuthorize("@ss.hasPermi('transportationcompany:system:add')")
    @Log(title = "运输公司绑定系统账号", businessType = BusinessType.INSERT)
    @PostMapping("/addTransportationCompanyUser")
    public AjaxResult addDriverUser(@RequestBody TransportationCompany transportationCompany)
    {
        SysUser user = new SysUser();
        //运输公司  负责人电话
        user.setUserName(transportationCompany.getPicContact());
        user.setPhonenumber(transportationCompany.getPicContact());
        user.setNickName(transportationCompany.getPicContact());
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        //司机默认密码 123456
        user.setPassword(SecurityUtils.encryptPassword("123456"));


        return toAjax(userService.insertTransportationCompanyUser(user,transportationCompany));
    }

}
